const Koa = require('koa');
const app = new Koa();
// 引入路由
const router = require('@koa/router')()

const bodyParser = require('koa-bodyparser');

// 处理get请求的接口
router.get('/list', async (ctx) => {
    // 获取get请求的参数 ctx.query
    let { page, pagesize } = ctx.query
    ctx.body = { code: 1, msg: "成功", page: page, pagesize: pagesize }
})

// 处理post请求的接口
router.post('/list/add', async (ctx) => {
    // 获取post请求的参数 ctx.request.body
    ctx.body = { code: 1, msg: "成功" }
    console.log(ctx.request.body);
})

// 获取动态路由
router.get('/detail/:id', async (ctx) => {
    let { id } = ctx.params
    console.log(ctx.params);
    ctx.body = { code: 1, msg: "成功", id }
})

app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());

// 启动
app.listen(4000, () => {
    console.log("http://localhost:4000 启动成功");
});